package com.woniuxy.yoga.progress.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.yoga.commons.core.model.Result;
import com.woniuxy.yoga.progress.client.OrderClient;
import com.woniuxy.yoga.progress.dao.model.Teacard;
import com.woniuxy.yoga.progress.dao.mapper.TeacardMapper;
import com.woniuxy.yoga.progress.model.exception.ProgressException;
import com.woniuxy.yoga.progress.model.exception.ProgressExceptionCode;
import com.woniuxy.yoga.progress.model.param.CreateTeaCardParam;
import com.woniuxy.yoga.progress.service.TeacardService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Objects;

/**
 * <p>
 * 团课流程表 服务实现类
 * </p>
 *
 * @author 韦用文,刘存益,郎腾霄
 * @since 2023年11月28日
 */
@Service
public class TeacardServiceImpl extends ServiceImpl<TeacardMapper, Teacard> implements TeacardService {

    @Autowired
    private TeacardMapper teacardMapper;

    @Autowired
    private OrderClient orderClient;

    /**
     * 创建团课进度表
     * @param param 创建团课进度表参数
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createTeaCard(CreateTeaCardParam param) {
        //检查是否重复添加
        QueryWrapper<Teacard> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",param.getOrderNum());
        Teacard teacard = teacardMapper.selectOne(wrapper);
        if (teacard!=null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_CARD_REPEAT);
        //创建团课进度对象
        Teacard card = new Teacard();
        card.setOrderNum(param.getOrderNum());
        card.setUserId(param.getUserId());
        card.setTeamHour(param.getHourCount());
        card.setTeacardUse(0);
        teacardMapper.insert(card);
    }

    /**
     * 完成团课
     * @param teacardId 团课进度id
     */
    @Override
    @GlobalTransactional(rollbackFor = Exception.class)
    public void finishTeacard(Integer teacardId) {
        Teacard teacard = teacardMapper.selectById(teacardId);
        //检查团课是否存在
        if (teacard == null) throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_CARD_NOT_EXIST);
        //是否完成
        if (!Objects.equals(teacard.getTeamHour(), teacard.getTeacardUse()))
            throw new ProgressException(ProgressExceptionCode.PROGRESS_EXCEPTION_CODE_TEA_CARD_NOT_USED);
        //修改订单状态
        Result<String> result = orderClient.completeOrder(teacard.getOrderNum());
        if (result.getCode()!=200) throw new ProgressException(result.getCode(),result.getMessage());
        //修改
        teacard.setCompleteTime(DateUtil.format(new Date(System.currentTimeMillis()),"yyyy-MM-dd HH:mm:ss"));
        teacardMapper.updateById(teacard);
    }
}
